package com.hankol.material.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.baselibrary.utils.GlideUtils
import com.baselibrary.utils.HKUtils
import com.baselibrary.view.RoundCornerImageView
import com.hankol.material.R
import com.hankol.material.api.MaterialConstants
import com.hankol.material.bean.MaterialContentInfo

/**
 * @description:
 * @author: 关忆北丶
 * @date :   2021/10/18 16:03
 */
class ContentListImgAdapter : RecyclerView.Adapter<ContentListImgAdapter.ContentListImgHolder>() {
    private var mContext: Context? = null
    var mList: ArrayList<MaterialContentInfo.FileLibsBean.DataBean> = arrayListOf()
    private var position = -1
    private var isFromCollect = false
    var collectionFolderId: Long = 0
    fun setData(list: List<MaterialContentInfo.FileLibsBean.DataBean>, isFromCollect: Boolean = false) {
        this.isFromCollect = isFromCollect
        mList.clear()
        mList.addAll(list)
        notifyDataSetChanged()
    }

    fun addData(list: List<MaterialContentInfo.FileLibsBean.DataBean>, isFromCollect: Boolean = false) {
        this.isFromCollect = isFromCollect
        if (list.isNotEmpty()) {
            mList.addAll(list)
            notifyDataSetChanged()
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContentListImgHolder {
        mContext = parent.context
        val inflate =
            LayoutInflater.from(mContext).inflate(R.layout.item_contentlist_img, parent, false)
        return ContentListImgHolder(inflate)
    }

    override fun onBindViewHolder(holder: ContentListImgHolder, position: Int) {
        val dataBean = mList[position]
        dataBean.collectionFolderId = collectionFolderId
        GlideUtils.loadImg_101(mContext, dataBean.thumbnail, holder.mRcImg)

        holder.itemView.setOnClickListener {
            if (!HKUtils.isFastClick()) return@setOnClickListener
            mContext?.let { it1 ->
                //打开图片详情
                MaterialConstants.openImgDetail(it1, mList, position, isFromCollect)
                this.position = position
            }
        }
    }


    fun removeCurrentData() {
        if (position != -1) {
            mList.removeAt(position)
            notifyDataSetChanged()
        }
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    inner class ContentListImgHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mRcImg: RoundCornerImageView = itemView.findViewById(R.id.rc_img)

    }
}
